package com.example.administrator.textapp;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.EditText;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    private RadioGroup mRadioGroup;
    private Boolean isSummer=null;
    private EditText etInput;
    private TextView tvResult;
    private double result=0;
    @Override
    protected void onCreate(final Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = findViewById(R.id.toolbar);
        TextView textView = findViewById(R.id.toolbar_title);
        textView.setText(R.string.app_name);
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayShowTitleEnabled(false);
        mRadioGroup = findViewById(R.id.radio_group);
        etInput = findViewById(R.id.et_input);
        tvResult = findViewById(R.id.tv_result);

        mRadioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                switch (checkedId){
                    case R.id.rb_summer:
                        isSummer=true;
                        Toast.makeText(MainActivity.this,"您选择了夏季",Toast.LENGTH_SHORT).show();
                        break;
                    case R.id.rb_winter:
                        isSummer = false;
                        Toast.makeText(MainActivity.this,"您选择了非夏季",Toast.LENGTH_SHORT).show();
                        break;
                    default:
                }
            }
        });

        findViewById(R.id.btn_cos).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String input = etInput.getText().toString();
                if(isSummer!=null && !input.equals("")){
                    int in = Integer.valueOf(input);
                    if(isSummer){
                      if(in>=0&&in<=260){
                          result = in*0.61;
                      }else if(in>=261&&in<=600){
                          result = (in-260)*0.66+260*0.61;
                      }else if(in>=601){
                          result = (in-601)*0.91+(600-260)*0.66+260*0.61;
                      }
                    }else {
                        if(in>=0&&in<=200){
                            result = in*0.61;
                        }else if(in>=201&&in<=400){
                            result = (in-200)*0.66+200*0.61;
                        }else if(in>=401){
                            result = (in-400)*0.91+(400-200)*0.66+200*0.61;
                        }
                    }
                }
                if(result!=0){
                    tvResult.setText(result+"");
                }
            }
        });


        findViewById(R.id.btn_cal).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }
}
